package team.yidong.fenda.controller;

import net.sf.json.JSONArray;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import team.yidong.fenda.entity.Hospital;
import team.yidong.fenda.service.HospitalDepartmentService;
import team.yidong.fenda.service.HospitalService;

import javax.servlet.http.HttpSession;

/**
 * Created by xuanlong on 2018/3/24.
 */
@Controller
public class HospitalDepartmentController {
    @Autowired
    HospitalDepartmentService hospitalDepartmentService;
    @Autowired
    HospitalService hospitalService;

    @RequestMapping("/hospitalDepartment")

    public String hospitalDepartment(Model model) {
        JSONArray jsonArray = JSONArray.fromObject(hospitalService.findAll());
        jsonArray.remove(0);
        model.addAttribute("hospitals", jsonArray);
        return "hospitalBumen";

    }

    @RequestMapping("/findHospitalDepartment")
    @ResponseBody
    public JSONArray findHospitalDepartment(String hospitalName) {

        return hospitalDepartmentService.findAll(hospitalName);
    }

    @RequestMapping("/findDepartmentByHospitalId")
    @ResponseBody
    public JSONArray findDepartmentByHospitalId(HttpSession session) {
        Integer hospitalId = (Integer) session.getAttribute("loginHospitalId");
        return hospitalDepartmentService.findHospitalDepartment(hospitalId);
    }

    @RequestMapping("/addHospitalDepartment")
    @ResponseBody
    public void addHospitalDepartment(@RequestParam String hospitalName, @RequestParam String departmentName) {
        hospitalDepartmentService.addHospitalDepartment(hospitalName, departmentName);
    }

    @RequestMapping("/findDepartmentByHospitalName")
    @ResponseBody
    public JSONArray findDepartmentByHospitalName(@RequestParam String hospitalName) {
        return hospitalDepartmentService.findByHospitalName(hospitalName);
    }
}
